﻿Imports TepcelDominio
Imports TepcelBL
Public Class frmUbigeo
    Public Shared Property tipoUbigeo As Integer
    Public Shared Property instanciaDepartamento As frmUbigeo = Nothing
    Public Shared Property instanciaProvincia As frmUbigeo = Nothing
    Public Shared Property instanciaDistrito As frmUbigeo = Nothing

    Dim err As New System.Windows.Forms.ErrorProvider
    Dim listaUbigeo As New List(Of Ubigeo)
    Dim UbigeoBL As New UbigeoBL

    Public Shared Function Instance(ByVal tipUbi As Integer) As frmUbigeo
        Dim frmInstance As frmUbigeo = Nothing
        Select Case tipUbi
            Case 1
                If instanciaDepartamento Is Nothing OrElse instanciaDepartamento.IsDisposed = True Then
                    instanciaDepartamento = New frmUbigeo
                End If
                frmInstance = instanciaDepartamento
            Case 2
                If instanciaProvincia Is Nothing OrElse instanciaProvincia.IsDisposed = True Then
                    instanciaProvincia = New frmUbigeo
                End If
                frmInstance = instanciaProvincia
            Case 3
                If instanciaDistrito Is Nothing OrElse instanciaDistrito.IsDisposed = True Then
                    instanciaDistrito = New frmUbigeo
                End If
                frmInstance = instanciaDistrito
        End Select

        frmInstance.BringToFront()

        Return frmInstance
    End Function

    Private Sub frmUbigeo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Select Case tipoUbigeo
            Case 1
                Me.Text = "Departamentos"
                CBEProvincia.Visible = False
                lbProvincia.Visible = False
                CBEDepartamento.Visible = False
                lbDepartamento.Visible = False
                GBNuevo.Height = GBNuevo.Height - 30 * 2
                GBLista.Top = GBNuevo.Top + GBNuevo.Height + 6
                Me.Height = Me.Height - 30 * 2
            Case 2
                Me.Text = "Provincias"
                CBEProvincia.Visible = False
                lbProvincia.Visible = False
                CBEDepartamento.Enabled = False
                GBNuevo.Height = GBNuevo.Height - 30
                GBLista.Top = GBNuevo.Top + GBNuevo.Height + 6
                Me.Height = Me.Height - 30
            Case 3
                Me.Text = "Distritos"
                CBEDepartamento.Enabled = False
                CBEProvincia.Enabled = False
        End Select

        Select Case tipoUbigeo
            Case 2
                DGVLista.Columns.Add("departamento", "Departamento")
                DGVLista.Columns(2).HeaderText = "Provincia"
            Case 3
                DGVLista.Columns.Add("provincia", "Provincia")
                DGVLista.Columns.Add("departamento", "Departamento")
                DGVLista.Columns(2).HeaderText = "Distrito"
        End Select

        Call CargarGrid()
        txtBuscar.Focus()

    End Sub


    Private Sub clearError()
        err.SetError(txtNombreUbigeo, "")
        err.SetError(CBEDepartamento, "")
        err.SetError(CBEProvincia, "")
    End Sub

    Private Sub CargarGrid()
        DGVLista.Rows.Clear()
        listaUbigeo = UbigeoBL.GetAllFromUbigeo(tipoUbigeo)
        If listaUbigeo.Count = 0 Then
            btnModificar.Enabled = False
            btnEliminar.Enabled = False
        End If
        
        Dim i As Integer
        For i = 0 To listaUbigeo.Count - 1
            With listaUbigeo.Item(i)
                Select Case tipoUbigeo
                    Case 1
                        DGVLista.Rows.Add(i, .codigoUbigeo, .nombreUbigeo)
                    Case 2
                        DGVLista.Rows.Add(i, .codigoUbigeo, .nombreUbigeo, .dependenciaUbigeo.nombreUbigeo)
                    Case 3
                        DGVLista.Rows.Add(i, .codigoUbigeo, .nombreUbigeo, .dependenciaUbigeo.nombreUbigeo, .dependenciaUbigeo.dependenciaUbigeo.nombreUbigeo)
                End Select

            End With
        Next
    End Sub

    Private Sub nuevo()
        btnModificar.Enabled = False
        btnEliminar.Enabled = False
        btnCancelar.Enabled = True
        btnGuardar.Enabled = True
        btnNuevo.Enabled = False

        Select Case tipoUbigeo
            Case 1
                txtNombreUbigeo.Enabled = True
                txtNombreUbigeo.Focus()
            Case 2
                CBEDepartamento.Enabled = True
                txtNombreUbigeo.Enabled = True
                Call CargarListaDeparatmentos()
                CBEDepartamento.Focus()
            Case 3
                CBEDepartamento.Enabled = True
                CBEProvincia.Enabled = True
                txtNombreUbigeo.Enabled = True
                Call CargarListaDeparatmentos()

                CBEDepartamento.Focus()
        End Select


        DGVLista.Enabled = False


    End Sub

    Private Sub CargarListaDeparatmentos()
        Dim listaDepartamento As New List(Of Ubigeo)

        listaDepartamento = UbigeoBL.GetAllFromUbigeo(1)

        CBEDepartamento.DataSource = Nothing
        CBEDepartamento.DataSource = listaDepartamento
        CBEDepartamento.DisplayMember = "nombreUbigeo"
        CBEDepartamento.ValueMember = "codigoUbigeo"

        If tipoUbigeo = 3 Then
            Call CargarListaProvincias()
        End If
    End Sub

    Private Sub CargarListaProvincias()
        Try
            Dim val As String = CBEDepartamento.SelectedValue
        Catch ex As Exception
            Return
        End Try

        Dim listaProvincia As New List(Of Ubigeo)
        listaProvincia = UbigeoBL.GetAllFromUbigeo(2)
        Dim i As Integer = 0
        While i < listaProvincia.Count
            If listaProvincia.Item(i).dependenciaUbigeo.codigoUbigeo <> CBEDepartamento.SelectedValue Then
                listaProvincia.RemoveAt(i)
                i = i - 1
            End If
            i = i + 1
        End While
        CBEProvincia.DataSource = Nothing
        CBEProvincia.DataSource = listaProvincia
        CBEProvincia.DisplayMember = "nombreUbigeo"
        CBEProvincia.ValueMember = "codigoUbigeo"

    End Sub

    Private Sub cancelar()
        btnModificar.Enabled = True
        btnEliminar.Enabled = True
        btnCancelar.Enabled = False
        btnGuardar.Enabled = False
        btnNuevo.Enabled = True

        Select Case tipoUbigeo
            Case 1
                txtNombreUbigeo.Enabled = False
            Case 2
                CBEDepartamento.Enabled = False
                txtNombreUbigeo.Enabled = False
                CBEDepartamento.Text = ""
            Case 3
                CBEDepartamento.Enabled = False
                CBEProvincia.Enabled = False
                txtNombreUbigeo.Enabled = False
                CBEDepartamento.Text = ""
                CBEProvincia.Text = ""
        End Select

        DGVLista.Enabled = True

        txtCodigoUbigeo.Text = ""
        txtNombreUbigeo.Text = ""

    End Sub

    Private Sub txtBuscar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.TextChanged
        If txtBuscar.Text.Trim.Length = 0 Then
            Call CargarGrid()
        Else
            DGVLista.Rows.Clear()
            listaUbigeo = UbigeoBL.SearchFromUbigeo(txtBuscar.Text.Trim, tipoUbigeo)
            Dim i As Integer
            For i = 0 To listaUbigeo.Count - 1
                With listaUbigeo.Item(i)
                    Select Case tipoUbigeo
                        Case 1
                            DGVLista.Rows.Add(i, .codigoUbigeo, .nombreUbigeo)
                        Case 2
                            DGVLista.Rows.Add(i, .codigoUbigeo, .nombreUbigeo, .dependenciaUbigeo.nombreUbigeo)
                        Case 3
                            DGVLista.Rows.Add(i, .codigoUbigeo, .nombreUbigeo, .dependenciaUbigeo.nombreUbigeo, .dependenciaUbigeo.dependenciaUbigeo.nombreUbigeo)
                    End Select
                End With
            Next
        End If
    End Sub

    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click
        Call nuevo()
        Call clearError()
        Dim item As Integer = DGVLista.Item(0, DGVLista.CurrentRow.Index).Value
        With listaUbigeo.Item(item)
            Select Case tipoUbigeo
                Case 1
                    txtCodigoUbigeo.Text = .codigoUbigeo
                    txtNombreUbigeo.Text = .nombreUbigeo
                    txtNombreUbigeo.Enabled = True
                Case 2
                    txtCodigoUbigeo.Text = .codigoUbigeo
                    CBEDepartamento.SelectedValue = .dependenciaUbigeo.codigoUbigeo
                    txtNombreUbigeo.Text = .nombreUbigeo
                    CBEDepartamento.Enabled = True
                    txtNombreUbigeo.Enabled = True
                Case 3
                    txtCodigoUbigeo.Text = .codigoUbigeo
                    CBEProvincia.SelectedValue = .dependenciaUbigeo.codigoUbigeo
                    CBEDepartamento.SelectedValue = .dependenciaUbigeo.dependenciaUbigeo.codigoUbigeo
                    txtNombreUbigeo.Text = .nombreUbigeo
                    CBEDepartamento.Enabled = True
                    CBEProvincia.Enabled = True
                    txtNombreUbigeo.Enabled = True
            End Select
        End With

    End Sub

    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If (txtCodigoUbigeo.Text.Trim.Length = 0) Then
            txtCodigoUbigeo.Text = ""
        End If
        Dim ubi As Ubigeo = Nothing
        Select Case tipoUbigeo
            Case 1
                If txtNombreUbigeo.Text.Trim.Length = 0 Then
                    err.SetError(txtNombreUbigeo, "LLene el nombre del Departamento")
                    Return
                Else
                    err.SetError(txtNombreUbigeo, "")
                End If
                ubi = New Ubigeo(txtCodigoUbigeo.Text.Trim, Nothing, txtNombreUbigeo.Text.Trim)
            Case 2
                If CBEDepartamento.SelectedValue.Trim.Length = 0 Then
                    err.SetError(CBEDepartamento, "Seleccione un Departamento")
                    Return
                Else
                    err.SetError(CBEDepartamento, "")
                End If
                If txtNombreUbigeo.Text.Trim.Length = 0 Then
                    err.SetError(txtNombreUbigeo, "LLene el nombre del Departamento")
                    Return
                Else
                    err.SetError(txtNombreUbigeo, "")
                End If
                ubi = New Ubigeo(txtCodigoUbigeo.Text.Trim, New Ubigeo(CBEDepartamento.SelectedValue, Nothing, CBEDepartamento.SelectedText), txtNombreUbigeo.Text.Trim)
            Case 3
                If CBEDepartamento.SelectedValue.Trim.Length = 0 Then
                    err.SetError(CBEDepartamento, "Seleccione un Departamento")
                    Return
                Else
                    err.SetError(CBEDepartamento, "")
                End If
                If CBEProvincia.SelectedValue.Trim.Length = 0 Then
                    err.SetError(CBEProvincia, "Seleccione una Provincia")
                    Return
                Else
                    err.SetError(CBEProvincia, "")
                End If
                If txtNombreUbigeo.Text.Trim.Length = 0 Then
                    err.SetError(txtNombreUbigeo, "LLene el nombre del Departamento")
                    Return
                Else
                    err.SetError(txtNombreUbigeo, "")
                End If
                ubi = New Ubigeo(txtCodigoUbigeo.Text.Trim, New Ubigeo(CBEProvincia.SelectedValue, Nothing, CBEProvincia.SelectedText), txtNombreUbigeo.Text.Trim)
        End Select
        txtCodigoUbigeo.Text = UbigeoBL.NuevoUbigeo(ubi)

        Call cancelar()
        Call CargarGrid()

    End Sub

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        Dim result As DialogResult
        result = MessageBox.Show("confirma que desea Eliminar el Registro seleccionado?", "Eliminar", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
        If result = DialogResult.OK Then
            If UbigeoBL.DeleteUbigeo(listaUbigeo.Item(CInt(DGVLista.Item(0, DGVLista.CurrentRow.Index).Value)).codigoUbigeo) Then
                MessageBox.Show("Registro Eliminado Correctamente", "Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Call CargarGrid()
            Else
                MessageBox.Show("No se elimino el registro seleccionado", "Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Call cancelar()
        Call clearError()

        txtBuscar.Focus()

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        Call nuevo()
        Call clearError()
    End Sub

    Private Sub CBEDepartamento_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CBEDepartamento.KeyUp
        Dim sTexto As String
        Dim indiceEncontrado As Integer
        Dim itemEncontrado As Object
        Dim textoItem As String
        Dim agregarTexto As String


        Select Case e.KeyCode
            Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down, Keys.Tab
                Return
        End Select

        sTexto = CBEDepartamento.Text
        indiceEncontrado = CBEDepartamento.FindString(sTexto)

        If indiceEncontrado >= 0 Then
            itemEncontrado = CBEDepartamento.Items(indiceEncontrado)
            textoItem = CBEDepartamento.GetItemText(itemEncontrado)

            agregarTexto = textoItem.Substring(sTexto.Length)
            CBEDepartamento.Text = sTexto & agregarTexto

            CBEDepartamento.SelectionStart = sTexto.Length
            CBEDepartamento.SelectionLength = agregarTexto.Length

        End If

    End Sub

    Private Sub CBEDepartamento_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBEDepartamento.SelectedIndexChanged

        Call CargarListaProvincias()
    End Sub

    Private Sub CBEProvincia_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CBEProvincia.KeyUp
        Dim sTexto As String
        Dim indiceEncontrado As Integer
        Dim itemEncontrado As Object
        Dim textoItem As String
        Dim agregarTexto As String


        Select Case e.KeyCode
            Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down, Keys.Tab
                Return
        End Select

        sTexto = CBEProvincia.Text
        indiceEncontrado = CBEProvincia.FindString(sTexto)

        If indiceEncontrado >= 0 Then
            itemEncontrado = CBEProvincia.Items(indiceEncontrado)
            textoItem = CBEProvincia.GetItemText(itemEncontrado)

            agregarTexto = textoItem.Substring(sTexto.Length)
            CBEProvincia.Text = sTexto & agregarTexto

            CBEProvincia.SelectionStart = sTexto.Length
            CBEProvincia.SelectionLength = agregarTexto.Length

        End If
    End Sub

    
    
End Class